package com.hc.android.research.component;

import java.util.ArrayList;
import java.util.List;

import com.hc.android.research.R;
import com.hc.android.research.dom.Item;
import com.hc.android.research.dom.ItemPrice;

import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ItemView extends TableLayout
{
	private Item _item = null;
	
	private TextView urlDescriptionTxt;
	private TextView urlInputTxt;
	private TextView titleDescriptionTxt;
	private TextView titleInputTxt;
	private TextView historyDescriptionTxt;
	private ListView historyList;

	public ItemView(Context context) 
	{
		super(context);	
		
		TableRow urlRow = new TableRow(context);
		urlRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
		urlDescriptionTxt = new TextView(context);
		urlInputTxt = new TextView(context);
		
		TableRow titleRow = new TableRow(context);
		titleRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
		titleDescriptionTxt = new TextView(context);
		titleInputTxt = new TextView(context);
		
		TableRow histDescRow = new TableRow(context);
		histDescRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
		historyDescriptionTxt = new TextView(context);
		
		TableRow histListRow = new TableRow(context);
		histListRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
		historyList = new ListView(context);
		
		urlRow.addView(urlDescriptionTxt);
		urlRow.addView(urlInputTxt);
		titleRow.addView(titleDescriptionTxt);
		titleRow.addView(titleInputTxt);
		histDescRow.addView(historyDescriptionTxt);
		histListRow.addView(historyList);
		
		addView(urlRow);
		addView(titleRow);
		addView(histDescRow);
		addView(histListRow);
	}
	
	public void setItem(Item item, Context context)
	{
		_item = item;
		
		urlDescriptionTxt.setText(_item.getItemURL().toString());
		titleDescriptionTxt.setText(_item.getItemName());
		
		List<ItemPrice> itemPrices = _item.getPrices();
		if(itemPrices != null)
		{
			List<TextView> textViewList = new ArrayList<TextView>();
			
			for(int i = 0; i < itemPrices.size(); i++)
			{
				ItemPrice itemPrice = itemPrices.get(i);
				TextView itemText = new TextView(context);
				itemText.setText(itemPrice.getPriceDate() + " - " + itemPrice.getItemPrice());
				textViewList.add(itemText);
			
			}
			
			historyList.setAdapter(new ArrayAdapter<TextView>(context, R.id.smallTextView, textViewList));
		}
	}
	
	public Item getItem()
	{
		return _item;
	}
}
